import { Product } from "../../public/Product"

//具体产品类 1
class ConcreateProduct1 extends Product {
    constructor() {
        super()
    }

}
//具体产品类 2
class ConcreateProduct2 extends Product {
    constructor() {
        super()
    }
}

let ProductStorage: any = {
    ConcreateProduct1,
    ConcreateProduct2,
}

//抽象工厂方法
abstract class AbstractFactoryMethod {
    public abstract create(type: string): any
}

//具体工厂方法 (根据具体需求继承抽象工厂方法生成具体实例)
class ConcreateFactoryMethod extends AbstractFactoryMethod {
    public create(type: string): Product {
        let product: Product
        if (ProductStorage[type]) {
            product = new (ProductStorage[type])()
        } else {
            throw new Error(`class ${type} dose not exits`)
        }
        return product
    }

    constructor() {
        super()
    }
}

let FactoryMethodTest = (type: string): void => {
    let concreateFactoryMethod: ConcreateFactoryMethod = new ConcreateFactoryMethod();
    let product: Product = concreateFactoryMethod.create(type)
    product.price = 100
    console.log(product);
}

FactoryMethodTest('ConcreateProduct1')
